import pymongo
from pyecharts import options as opts
from pyecharts.charts import Page, Pie


db = pymongo.MongoClient('localhost',27017)
douban_db = db['doubantop250']
item_info = douban_db['movietop250']

category_list=[]
for i in item_info.find():
    category_list.append(str(i['category']).strip())


movie_type = ['爱情', '犯罪', '惊悚', '动画', '科幻', '战争', '喜剧', '动作']
count_list = []
for j in range(0, 8):
    count = 0
    for i in category_list:
        if movie_type[j] in i:
            count += 1
    count_list.append(count)


pie = Pie()
pie.add(
    "",
    [z for z in zip(movie_type, count_list)],
    radius=["40%", "75%"],
    center=["50%", "50%"],
    rosetype="radius"
)

pie.set_global_opts(
    title_opts=opts.TitleOpts(title="豆瓣top250电影类型数量占比"),
    legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="6%")
)

pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))

pie.render('mycategory1.0.html')